﻿'         This file is Copyright © 2008-2012 Billy O'Neal and Aommaster
'                            All rights reserved.
'
' This file is released under the 2-Clause BSD license, included in LICENSE.TXT
'                           and on the MHWT website
Friend Class Normalizer
    Private usedRegex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("(?<attributes>[acdfhrsvw-]+)(?: \| [CM]\](?: \(.*?\))? --)?[\s]*(?<file>[A-Z]:.*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

    Private Sub btnNormalize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNormalize.Click
        Try
            Dim folders As String = ""
            Dim files As String = ""
            Dim linetowrite As String = ""
            If boxOutput.SelectedItem Is Nothing Then
                MsgBox("Please pick an output mode first.")
                Exit Sub
            End If
            If getfile.Checked Then
                linetowrite = ""
                Dim fileregex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("\w{1}:\\.*\.?\w{1,4}")
                For Each line As System.Text.RegularExpressions.Match In fileregex.Matches(textBoxInOut.Text)
                    If InStr(linetowrite, line.ToString) = 0 Then
                        linetowrite += line.ToString + Environment.NewLine
                    End If
                Next
                textBoxInOut.Text = linetowrite
            Else
                For Each line As System.Text.RegularExpressions.Match In usedRegex.Matches(textBoxInOut.Text)
                    If line.Groups("attributes").Value.Contains("d") Or line.Groups("attributes").Value.Contains("D") Then
                        linetowrite = System.Text.RegularExpressions.Regex.Replace(line.Groups("file").Value.Trim(), ".*([a-zA-Z]\:\\[^:*?""<>|\r\n]+).*", "$1")

                        If InStr(folders, linetowrite) = 0 Then
                            folders += linetowrite + Environment.NewLine
                        End If

                    Else
                        linetowrite = System.Text.RegularExpressions.Regex.Replace(line.Groups("file").Value.Trim(), ".*([a-zA-Z]\:\\[^:*?""<>|\r\n]+).*", "$1")

                        If InStr(files, linetowrite) = 0 Then
                            files += linetowrite + Environment.NewLine
                        End If

                    End If
                Next
                textBoxInOut.Text = ""
                If boxOutput.SelectedItem Is "CFScript" Then
                    If Not folders.Length = 0 Then textBoxInOut.Text += "folder::" + Environment.NewLine + folders
                    If Not files.Length = 0 Then textBoxInOut.Text += "file::" + Environment.NewLine + files
                ElseIf boxOutput.SelectedItem Is "Avenger" Then
                    If Not folders.Length = 0 Then textBoxInOut.Text += "folders to delete:" + Environment.NewLine + folders
                    If Not files.Length = 0 Then textBoxInOut.Text += "files to delete:" + Environment.NewLine + files
                ElseIf boxOutput.SelectedItem Is "OTMIScript" Then
                    If Not folders.Length = 0 Or Not files.Length = 0 Then
                        textBoxInOut.Text += ":files" + Environment.NewLine + folders + files
                    End If
                End If
                textBoxInOut.Text = textBoxInOut.Text.Replace("\[u]0[/u]", "\0")
                If toClip.Checked Then
                    My.Computer.Clipboard.SetText(textBoxInOut.Text)
                End If
            End If

        Catch ex As Exception
            MsgBox("This text cannot be normalized. Note that only folders and files can be normalized")
        End Try


    End Sub
    Private Sub textBoxInOut_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textBoxInOut.KeyDown
        If e.Control Then
            If e.KeyCode = Keys.A Then
                e.SuppressKeyPress = True
                textBoxInOut.SelectAll()
            End If
        End If
    End Sub

    Private Sub Normalizer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        boxOutput.SelectedIndex = 0
    End Sub
End Class